<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>操作数据库 | Guru Note</title>
    <meta name="generator" content="VuePress 1.7.1">
    <link rel="stylesheet" href="./font/index.css">
    <script src="/docs/icon/svg.js"></script>
    <meta name="description" content="Sea of dawn">
    
    <link rel="preload" href="/docs/assets/css/0.styles.efa081cd.css" as="style"><link rel="preload" href="/docs/assets/js/app.cdec4db9.js" as="script"><link rel="preload" href="/docs/assets/js/2.993bd611.js" as="script"><link rel="preload" href="/docs/assets/js/95.4f07ca32.js" as="script"><link rel="preload" href="/docs/assets/js/7.5a12abef.js" as="script"><link rel="prefetch" href="/docs/assets/js/10.c3177bdb.js"><link rel="prefetch" href="/docs/assets/js/100.60c23478.js"><link rel="prefetch" href="/docs/assets/js/101.7ea29e1b.js"><link rel="prefetch" href="/docs/assets/js/102.7f47e485.js"><link rel="prefetch" href="/docs/assets/js/103.ea9ce400.js"><link rel="prefetch" href="/docs/assets/js/104.4ec6a518.js"><link rel="prefetch" href="/docs/assets/js/105.75526347.js"><link rel="prefetch" href="/docs/assets/js/106.01f6b03b.js"><link rel="prefetch" href="/docs/assets/js/107.69fe0811.js"><link rel="prefetch" href="/docs/assets/js/108.fa23768b.js"><link rel="prefetch" href="/docs/assets/js/109.90df1698.js"><link rel="prefetch" href="/docs/assets/js/11.54347528.js"><link rel="prefetch" href="/docs/assets/js/110.dd8d7227.js"><link rel="prefetch" href="/docs/assets/js/111.ccf25ceb.js"><link rel="prefetch" href="/docs/assets/js/112.6963298f.js"><link rel="prefetch" href="/docs/assets/js/113.30ceb3d8.js"><link rel="prefetch" href="/docs/assets/js/114.42ef6603.js"><link rel="prefetch" href="/docs/assets/js/115.f1db7817.js"><link rel="prefetch" href="/docs/assets/js/116.92971223.js"><link rel="prefetch" href="/docs/assets/js/117.c5a65e7e.js"><link rel="prefetch" href="/docs/assets/js/118.e329035d.js"><link rel="prefetch" href="/docs/assets/js/119.cbb17c5e.js"><link rel="prefetch" href="/docs/assets/js/12.c8144ee8.js"><link rel="prefetch" href="/docs/assets/js/120.8537f6a6.js"><link rel="prefetch" href="/docs/assets/js/121.257d3851.js"><link rel="prefetch" href="/docs/assets/js/122.96a5f921.js"><link rel="prefetch" href="/docs/assets/js/123.2220fd12.js"><link rel="prefetch" href="/docs/assets/js/124.552b1a29.js"><link rel="prefetch" href="/docs/assets/js/125.4e36fe37.js"><link rel="prefetch" href="/docs/assets/js/126.3939cdcc.js"><link rel="prefetch" href="/docs/assets/js/127.014df434.js"><link rel="prefetch" href="/docs/assets/js/13.edc237a8.js"><link rel="prefetch" href="/docs/assets/js/14.66270d4f.js"><link rel="prefetch" href="/docs/assets/js/15.0804164b.js"><link rel="prefetch" href="/docs/assets/js/16.dec928ab.js"><link rel="prefetch" href="/docs/assets/js/17.bbfc90a8.js"><link rel="prefetch" href="/docs/assets/js/18.76904860.js"><link rel="prefetch" href="/docs/assets/js/19.80a98011.js"><link rel="prefetch" href="/docs/assets/js/20.c39e42bd.js"><link rel="prefetch" href="/docs/assets/js/21.287b744a.js"><link rel="prefetch" href="/docs/assets/js/22.c9506be7.js"><link rel="prefetch" href="/docs/assets/js/23.9732a229.js"><link rel="prefetch" href="/docs/assets/js/24.21ed1f05.js"><link rel="prefetch" href="/docs/assets/js/25.8f1926f6.js"><link rel="prefetch" href="/docs/assets/js/26.61b6eb9f.js"><link rel="prefetch" href="/docs/assets/js/27.3706753c.js"><link rel="prefetch" href="/docs/assets/js/28.b6b182cf.js"><link rel="prefetch" href="/docs/assets/js/29.ae979ad9.js"><link rel="prefetch" href="/docs/assets/js/3.a7f8dd77.js"><link rel="prefetch" href="/docs/assets/js/30.f2233269.js"><link rel="prefetch" href="/docs/assets/js/31.06780314.js"><link rel="prefetch" href="/docs/assets/js/32.2dad91d1.js"><link rel="prefetch" href="/docs/assets/js/33.87ec6e22.js"><link rel="prefetch" href="/docs/assets/js/34.7b75f220.js"><link rel="prefetch" href="/docs/assets/js/35.3184991f.js"><link rel="prefetch" href="/docs/assets/js/36.28248fbe.js"><link rel="prefetch" href="/docs/assets/js/37.5824a979.js"><link rel="prefetch" href="/docs/assets/js/38.99a364fe.js"><link rel="prefetch" href="/docs/assets/js/39.a5f43732.js"><link rel="prefetch" href="/docs/assets/js/4.a55d89ed.js"><link rel="prefetch" href="/docs/assets/js/40.629f78f7.js"><link rel="prefetch" href="/docs/assets/js/41.e4f58d1c.js"><link rel="prefetch" href="/docs/assets/js/42.9f41aa47.js"><link rel="prefetch" href="/docs/assets/js/43.636412b6.js"><link rel="prefetch" href="/docs/assets/js/44.3b491aef.js"><link rel="prefetch" href="/docs/assets/js/45.77df19bc.js"><link rel="prefetch" href="/docs/assets/js/46.01f53ddd.js"><link rel="prefetch" href="/docs/assets/js/47.476e85c9.js"><link rel="prefetch" href="/docs/assets/js/48.198502dc.js"><link rel="prefetch" href="/docs/assets/js/49.0d59d332.js"><link rel="prefetch" href="/docs/assets/js/5.1597c0f8.js"><link rel="prefetch" href="/docs/assets/js/50.02baf101.js"><link rel="prefetch" href="/docs/assets/js/51.c9fbd54d.js"><link rel="prefetch" href="/docs/assets/js/52.4cb4459b.js"><link rel="prefetch" href="/docs/assets/js/53.248450d7.js"><link rel="prefetch" href="/docs/assets/js/54.83f12d1e.js"><link rel="prefetch" href="/docs/assets/js/55.b18accba.js"><link rel="prefetch" href="/docs/assets/js/56.8a160b09.js"><link rel="prefetch" href="/docs/assets/js/57.b854a940.js"><link rel="prefetch" href="/docs/assets/js/58.4500f315.js"><link rel="prefetch" href="/docs/assets/js/59.59400e36.js"><link rel="prefetch" href="/docs/assets/js/6.887ba020.js"><link rel="prefetch" href="/docs/assets/js/60.4dd5b5bb.js"><link rel="prefetch" href="/docs/assets/js/61.50d8c8f6.js"><link rel="prefetch" href="/docs/assets/js/62.17d10daa.js"><link rel="prefetch" href="/docs/assets/js/63.d5f821cc.js"><link rel="prefetch" href="/docs/assets/js/64.7bf2519f.js"><link rel="prefetch" href="/docs/assets/js/65.0a1c9bc8.js"><link rel="prefetch" href="/docs/assets/js/66.1bcaa81c.js"><link rel="prefetch" href="/docs/assets/js/67.326bdf9b.js"><link rel="prefetch" href="/docs/assets/js/68.2b3b63b2.js"><link rel="prefetch" href="/docs/assets/js/69.c9b1a1a9.js"><link rel="prefetch" href="/docs/assets/js/70.a5fede78.js"><link rel="prefetch" href="/docs/assets/js/71.288d9643.js"><link rel="prefetch" href="/docs/assets/js/72.8665d6b8.js"><link rel="prefetch" href="/docs/assets/js/73.47155429.js"><link rel="prefetch" href="/docs/assets/js/74.4d4738f8.js"><link rel="prefetch" href="/docs/assets/js/75.659a325f.js"><link rel="prefetch" href="/docs/assets/js/76.a263f692.js"><link rel="prefetch" href="/docs/assets/js/77.9d86d59e.js"><link rel="prefetch" href="/docs/assets/js/78.a41b5d88.js"><link rel="prefetch" href="/docs/assets/js/79.5d0fc4cc.js"><link rel="prefetch" href="/docs/assets/js/8.80d0c81d.js"><link rel="prefetch" href="/docs/assets/js/80.01a11f6a.js"><link rel="prefetch" href="/docs/assets/js/81.9c0207d9.js"><link rel="prefetch" href="/docs/assets/js/82.12be8e04.js"><link rel="prefetch" href="/docs/assets/js/83.00fa5f46.js"><link rel="prefetch" href="/docs/assets/js/84.f14cd53c.js"><link rel="prefetch" href="/docs/assets/js/85.e9d7c0d1.js"><link rel="prefetch" href="/docs/assets/js/86.eb78fbbf.js"><link rel="prefetch" href="/docs/assets/js/87.8a31f5ae.js"><link rel="prefetch" href="/docs/assets/js/88.0f10e6dc.js"><link rel="prefetch" href="/docs/assets/js/89.a17253c9.js"><link rel="prefetch" href="/docs/assets/js/9.2fa568cb.js"><link rel="prefetch" href="/docs/assets/js/90.e56bc609.js"><link rel="prefetch" href="/docs/assets/js/91.5277db18.js"><link rel="prefetch" href="/docs/assets/js/92.b0bb8aa7.js"><link rel="prefetch" href="/docs/assets/js/93.cd790b77.js"><link rel="prefetch" href="/docs/assets/js/94.76bbe227.js"><link rel="prefetch" href="/docs/assets/js/96.2b1e8485.js"><link rel="prefetch" href="/docs/assets/js/97.e93b3461.js"><link rel="prefetch" href="/docs/assets/js/98.658063a1.js"><link rel="prefetch" href="/docs/assets/js/99.a3642e6d.js">
    <link rel="stylesheet" href="/docs/assets/css/0.styles.efa081cd.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/docs/" class="home-link router-link-active"><!----> <span class="site-name">Guru Note</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----> <svg aria-hidden="true" class="icon search-icon"><use xlink:href="#icon-search"></use></svg></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/docs/layout/" class="nav-link">
  CSS 手册
</a></div><div class="nav-item"><a href="/docs/js/" class="nav-link">
  JavaScript
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端进阶" class="dropdown-title"><span class="title">前端进阶</span> <span class="arrow down"></span></button> <button type="button" aria-label="前端进阶" class="mobile-dropdown-title"><span class="title">前端进阶</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          前端框架
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/docs/vue/" class="nav-link">
  Vue.js
</a></li><li class="dropdown-subitem"><a href="/docs/react/" class="nav-link">
  React.js
</a></li></ul></li><li class="dropdown-item"><h4>
          拓展语言
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/docs/node/" class="nav-link router-link-active">
  Node.js
</a></li><li class="dropdown-subitem"><a href="/docs/ts/" class="nav-link">
  TypeScript
</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/docs/devbook/" class="nav-link">
  开发手册
</a></div><div class="nav-item"><a href="/docs/exam/" class="nav-link">
  金九银十
</a></div><div class="nav-item"><a href="/docs/message/" class="nav-link">
  留言板
</a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><div class="el-scrollbar"><div class="scrollbar-wrapper el-scrollbar__wrap el-scrollbar__wrap--hidden-default"><div class="el-scrollbar__view"><nav class="nav-links"><div class="nav-item"><a href="/docs/layout/" class="nav-link">
  CSS 手册
</a></div><div class="nav-item"><a href="/docs/js/" class="nav-link">
  JavaScript
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="前端进阶" class="dropdown-title"><span class="title">前端进阶</span> <span class="arrow down"></span></button> <button type="button" aria-label="前端进阶" class="mobile-dropdown-title"><span class="title">前端进阶</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><h4>
          前端框架
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/docs/vue/" class="nav-link">
  Vue.js
</a></li><li class="dropdown-subitem"><a href="/docs/react/" class="nav-link">
  React.js
</a></li></ul></li><li class="dropdown-item"><h4>
          拓展语言
        </h4> <ul class="dropdown-subitem-wrapper"><li class="dropdown-subitem"><a href="/docs/node/" class="nav-link router-link-active">
  Node.js
</a></li><li class="dropdown-subitem"><a href="/docs/ts/" class="nav-link">
  TypeScript
</a></li></ul></li></ul></div></div><div class="nav-item"><a href="/docs/devbook/" class="nav-link">
  开发手册
</a></div><div class="nav-item"><a href="/docs/exam/" class="nav-link">
  金九银十
</a></div><div class="nav-item"><a href="/docs/message/" class="nav-link">
  留言板
</a></div> <!----></nav>  <ul class="sidebar-links"><li><a href="/docs/node/" aria-current="page" class="sidebar-link">介绍</a></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Node.js 基础</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/docs/node/technology-architecture.html" class="sidebar-link">Node.js 技术架构</a></li><li><a href="/docs/node/fs-file.html" class="sidebar-link">文件模块</a></li><li><a href="/docs/node/static-server.html" class="sidebar-link">静态服务器</a></li><li><a href="/docs/node/fanyi.html" class="sidebar-link">命令行翻译工具</a></li><li><a href="/docs/node/operational-database.html" aria-current="page" class="active sidebar-link">操作数据库</a></li></ul></section></li></ul> </div></div><div class="el-scrollbar__bar is-horizontal"><div class="el-scrollbar__thumb" style="width:0;transform:translateX(0%);ms-transform:translateX(0%);webkit-transform:translateX(0%);"></div></div><div class="el-scrollbar__bar is-vertical"><div class="el-scrollbar__thumb" style="height:0;transform:translateY(0%);ms-transform:translateY(0%);webkit-transform:translateY(0%);"></div></div></div></aside> <main class="page"> <div class="re-page-top"><h1 class="title"><span>操作数据库</span></h1> <div class="re-page-index"><div class="page-index-title"><span class="open-catalog"><svg aria-hidden="true" class="icon arrow-right"><use xlink:href="#icon-bold-right"></use></svg> <span class="text">目录</span></span></div> <div class="page-index-content"><div class="page-catalog" style="display:none;"><div class="synopsis-wrap"><ul class="synopsis-ul"><li class="synopsis-li"><a href="/docs/node/operational-database.html#安装-docker" class="synopsis-link">安装 Docker</a></li><li class="synopsis-li"><a href="/docs/node/operational-database.html#旧版-docker" class="synopsis-link">旧版 Docker</a></li><li class="synopsis-li"><a href="/docs/node/operational-database.html#安装-mysql" class="synopsis-link">安装 mysql</a></li><li class="synopsis-li"><a href="/docs/node/operational-database.html#docker-常用命令" class="synopsis-link">Docker 常用命令</a></li><li class="synopsis-li"><a href="/docs/node/operational-database.html#连接-mysql" class="synopsis-link">连接 mysql</a></li><li class="synopsis-li"><a href="/docs/node/operational-database.html#dbms" class="synopsis-link">DBMS</a></li><li class="synopsis-li"><a href="/docs/node/operational-database.html#使用-node-js-创建数据库" class="synopsis-link">使用 Node.js 创建数据库</a></li><li class="synopsis-li"><a href="/docs/node/operational-database.html#操作表内容" class="synopsis-link">操作表内容</a></li><li class="synopsis-li"><a href="/docs/node/operational-database.html#数据类型" class="synopsis-link">数据类型</a><ul class="synopsis-ul"><li class="synopsis-li"><a href="/docs/node/operational-database.html#关于问题搜索" class="synopsis-link">关于问题搜索</a></li></ul></li><li class="synopsis-li"><a href="/docs/node/operational-database.html#推荐文档" class="synopsis-link">推荐文档</a></li></ul></div></div></div></div></div> <div class="theme-default-content content__default"><h2 id="安装-docker"><a href="#安装-docker" class="header-anchor">#</a> 安装 Docker</h2> <ol><li>注册：<a href="https://hub.docker.com/" target="_blank" rel="noopener noreferrer">https://hub.docker.com/<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li>下载：<a href="https://docs.docker.com/docker-for-windows/install/" target="_blank" rel="noopener noreferrer">Docker for Windows Installer<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li>确认输入 <code>docker --version</code> 后返回版本号</li> <li>设置国内镜像，<a href="http://guide.daocloud.io/dcs/daocloud-9153151.html#Docker%E5%8A%A0%E9%80%9F%E5%99%A8-DockerforWindows" target="_blank" rel="noopener noreferrer">教程<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>，镜像的地址为：<code>https://docker.mirrors.ustc.edu.cn/</code></li> <li>确保命令行输入 <code>docker run hello-world</code> 时输出 Hello from Docker!</li> <li>如果无法安装新版，请查看旧版教程</li></ol> <p>新版配置镜像示例</p> <img src="/docs/images/docker-setting-mirror.jpg" alt="foo"> <h2 id="旧版-docker"><a href="#旧版-docker" class="header-anchor">#</a> 旧版 Docker</h2> <ol><li><a href="https://github.com/docker/toolbox/releases" target="_blank" rel="noopener noreferrer">下载 Docker Toolbox<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li>打开 Docker QuickStart，运行后看到一个IP，退出（运行 <code>docker-machine ip</code> 可得到 IP）</li> <li>重新打开命令行，输入 <code>docker -v</code> 查看是否正常</li> <li>设置国内镜像，<a href="http://guide.daocloud.io/dcs/daocloud-9153151.html#Docker%E5%8A%A0%E9%80%9F%E5%99%A8-DockerToolbox" target="_blank" rel="noopener noreferrer">教程<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>，镜像的地址为：<code>https://docker.mirrors.ustc.edu.cn/</code></li> <li>确保命令行输入 <code>docker run hello-world</code> 时输出 Hello from Docker!</li></ol> <h2 id="安装-mysql"><a href="#安装-mysql" class="header-anchor">#</a> 安装 mysql</h2> <p>创建一个 mysql 数据库</p> <div class="language-sh extra-class"><pre class="language-sh"><code>docker run --name mysql123 -e <span class="token assign-left variable">MYSQL_ROOT_PASSWORD</span><span class="token operator">=</span><span class="token number">123456</span> -p <span class="token number">3306</span>:3306 -d mysql:5.7.33
</code></pre></div><p>关于参数</p> <div class="language-sh extra-class"><pre class="language-sh"><code>--name   数据库名称
 -e      设置密码
 -p      设置端口
 -d      安装的 mysql 版本
</code></pre></div><h2 id="docker-常用命令"><a href="#docker-常用命令" class="header-anchor">#</a> Docker 常用命令</h2> <div class="language-sh extra-class"><pre class="language-sh"><code>docker <span class="token function">ps</span>                         <span class="token comment"># 查看运行状态</span>
docker <span class="token function">kill</span> mysql123              <span class="token comment"># 关掉指定容器（指定名称或ID）</span>
docker container start mysql123   <span class="token comment"># 开启刚刚关闭的容器</span>
docker <span class="token function">rm</span> mysql123                <span class="token comment"># 删除指定容器，-f 强制</span>
</code></pre></div><h2 id="连接-mysql"><a href="#连接-mysql" class="header-anchor">#</a> 连接 mysql</h2> <ol><li>进入容器</li></ol> <div class="language-sh extra-class"><pre class="language-sh"><code>docker <span class="token builtin class-name">exec</span> -it mysql123 <span class="token function">bash</span>
</code></pre></div><ol start="2"><li>输入账户密码</li></ol> <div class="language-sh extra-class"><pre class="language-sh"><code>mysql -u root -p
</code></pre></div><ol start="3"><li>查看数据库列表</li></ol> <div class="language-sh extra-class"><pre class="language-sh"><code>show databases<span class="token punctuation">;</span> <span class="token comment"># 必须加分号</span>

<span class="token comment"># 列表示例</span>
+--------------------+
<span class="token operator">|</span> Database           <span class="token operator">|</span>
+--------------------+
<span class="token operator">|</span> information_schema <span class="token operator">|</span>
<span class="token operator">|</span> mysql              <span class="token operator">|</span>
<span class="token operator">|</span> performance_schema <span class="token operator">|</span>
<span class="token operator">|</span> sys                <span class="token operator">|</span>
+--------------------+
<span class="token number">4</span> rows <span class="token keyword">in</span> <span class="token builtin class-name">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre></div><ol start="4"><li>使用其中一个数据库</li></ol> <div class="language-sh extra-class"><pre class="language-sh"><code>use mysql<span class="token punctuation">;</span>
</code></pre></div><ol start="5"><li>查看所有表</li></ol> <div class="language-sh extra-class"><pre class="language-sh"><code>show tables<span class="token punctuation">;</span>
</code></pre></div><ol start="6"><li>查看指定表的内容</li></ol> <div class="language-sh extra-class"><pre class="language-sh"><code><span class="token keyword">select</span> * from user<span class="token punctuation">;</span>
</code></pre></div><div class="custom-block tip"><p class="custom-block-title">注意</p> <p>Ctrl + D 可以退出当前环境</p></div> <h2 id="dbms"><a href="#dbms" class="header-anchor">#</a> DBMS</h2> <p>用来管理数据库，俗称数据库管理系统，例如：MySQL、<strong>PostgreSQL</strong>、SQL Server、DB2、Oracle</p> <p>DBMS 结构图</p> <img src="/docs/images/node-dbms.png" alt="DBMS"> <h2 id="使用-node-js-创建数据库"><a href="#使用-node-js-创建数据库" class="header-anchor">#</a> 使用 Node.js 创建数据库</h2> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> mysql      <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'mysql'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> connection <span class="token operator">=</span> mysql<span class="token punctuation">.</span><span class="token function">createConnection</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  host     <span class="token operator">:</span> <span class="token string">'localhost'</span><span class="token punctuation">,</span>
  user     <span class="token operator">:</span> <span class="token string">'root'</span><span class="token punctuation">,</span>
  password <span class="token operator">:</span> <span class="token string">'123456'</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

connection<span class="token punctuation">.</span><span class="token function">connect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 连接数据库</span>

<span class="token comment">/* 创建一个名为 cheng 的数据库 */</span>
connection<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token string">'CREATE DATABASE IF NOT EXISTS cheng DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci;'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">error<span class="token punctuation">,</span> results<span class="token punctuation">,</span> fields</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token keyword">throw</span> error<span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'创建数据库'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>results<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">/* 使用 cheng 数据库 */</span>
connection<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token string">'use cheng'</span><span class="token punctuation">)</span>
<span class="token comment">/* 创建一个名为 user 的表 */</span>
connection<span class="token punctuation">.</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">CREATE TABLE IF NOT EXISTS user(
  name text,
  age int
)</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">error<span class="token punctuation">,</span> results<span class="token punctuation">,</span> fields</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token keyword">throw</span> error<span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'创建表'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>results<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

connection<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>查看该表</p> <div class="language-sh extra-class"><pre class="language-sh"><code><span class="token comment"># 查看所有库</span>
$ show databases<span class="token punctuation">;</span>
+--------------------+
<span class="token operator">|</span> Database           <span class="token operator">|</span>
+--------------------+
<span class="token operator">|</span> information_schema <span class="token operator">|</span>
<span class="token operator">|</span> cheng              <span class="token operator">|</span>
<span class="token operator">|</span> mysql              <span class="token operator">|</span>
<span class="token operator">|</span> performance_schema <span class="token operator">|</span>
<span class="token operator">|</span> sys                <span class="token operator">|</span>
+--------------------+
<span class="token number">5</span> rows <span class="token keyword">in</span> <span class="token builtin class-name">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>

<span class="token comment"># 选择 cheng 数据库</span>
$ use cheng<span class="token punctuation">;</span>

<span class="token comment"># 显示数据库的表</span>
$ show tables<span class="token punctuation">;</span>
+-----------------+
<span class="token operator">|</span> Tables_in_cheng <span class="token operator">|</span>
+-----------------+
<span class="token operator">|</span> user            <span class="token operator">|</span>
+-----------------+
<span class="token number">1</span> row <span class="token keyword">in</span> <span class="token builtin class-name">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>

<span class="token comment"># 显示 user 表</span>
$ describe user<span class="token punctuation">;</span>                             
+-------+---------+------+-----+---------+-------+
<span class="token operator">|</span> Field <span class="token operator">|</span> Type    <span class="token operator">|</span> Null <span class="token operator">|</span> Key <span class="token operator">|</span> Default <span class="token operator">|</span> Extra <span class="token operator">|</span>
+-------+---------+------+-----+---------+-------+
<span class="token operator">|</span> name  <span class="token operator">|</span> text    <span class="token operator">|</span> YES  <span class="token operator">|</span>     <span class="token operator">|</span> NULL    <span class="token operator">|</span>       <span class="token operator">|</span>
<span class="token operator">|</span> age   <span class="token operator">|</span> int<span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span> <span class="token operator">|</span> YES  <span class="token operator">|</span>     <span class="token operator">|</span> NULL    <span class="token operator">|</span>       <span class="token operator">|</span>
+-------+---------+------+-----+---------+-------+
<span class="token number">2</span> rows <span class="token keyword">in</span> <span class="token builtin class-name">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>

<span class="token comment"># 删除 user 表</span>
DROP table user<span class="token punctuation">;</span>

<span class="token comment"># 删除 cheng 库</span>
drop database cheng<span class="token punctuation">;</span>
</code></pre></div><h2 id="操作表内容"><a href="#操作表内容" class="header-anchor">#</a> 操作表内容</h2> <div class="language-sh extra-class"><pre class="language-sh"><code><span class="token comment"># 在 user 表添加数据</span>
insert into user <span class="token punctuation">(</span>name, age<span class="token punctuation">)</span> values <span class="token punctuation">(</span><span class="token string">'ject'</span>, <span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment"># 修改 user 表中 name 为 ject 的数据，将它的 age 字段改为 70</span>
update user <span class="token builtin class-name">set</span> <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">70</span> where <span class="token assign-left variable">name</span><span class="token operator">=</span><span class="token string">'ject'</span><span class="token punctuation">;</span>

<span class="token comment"># 不要使用以下方式修改，会将数据库中所有 age字段都修改</span>
update user <span class="token builtin class-name">set</span> <span class="token assign-left variable">age</span><span class="token operator">=</span><span class="token number">18</span><span class="token punctuation">;</span>

<span class="token comment"># 查询 user 表所有字段</span>
<span class="token keyword">select</span> * from user<span class="token punctuation">;</span>

<span class="token comment"># 查询 user 表中的指定字段</span>
<span class="token keyword">select</span> name,age from user<span class="token punctuation">;</span>

<span class="token comment"># 只查询该字段的前十行</span>
<span class="token keyword">select</span> name,age from user limit <span class="token number">10</span><span class="token punctuation">;</span>
</code></pre></div><p>给表添加</p> <div class="language-sh extra-class"><pre class="language-sh"><code>
</code></pre></div><div class="custom-block tip"><p class="custom-block-title">注意</p> <p>查询命令只在表内有数据的情况下才会显示表</p></div> <h2 id="数据类型"><a href="#数据类型" class="header-anchor">#</a> 数据类型</h2> <ul><li>数字类型：<a href="https://dev.mysql.com/doc/refman/8.0/en/numeric-type-syntax.html" target="_blank" rel="noopener noreferrer">https://dev.mysql.com/doc/refman/8.0/en/numeric-type-syntax.html<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li>字符串类型</li> <li>时间和日期类型，推荐关注 ISO 8601 及 <a href="https://stackoverflow.com/questions/9321809/format-date-in-mysql-select-as-iso-8601" target="_blank" rel="noopener noreferrer">如何把日期输出为 ISO 8601 格式<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li>JSON 类型（5.7.8以上版本）</li> <li>其他特殊类型</li></ul> <p>使用 serial 类型（数字类型）添加用户 id 案例</p> <div class="language-sh extra-class"><pre class="language-sh"><code><span class="token comment"># 添加一列 id</span>
alter table user <span class="token function">add</span> <span class="token function">id</span> serial<span class="token punctuation">;</span>

<span class="token comment"># 查看列表</span>
describe user<span class="token punctuation">;</span>
+-------+---------------------+------+-----+---------+----------------+
<span class="token operator">|</span> Field <span class="token operator">|</span> Type                <span class="token operator">|</span> Null <span class="token operator">|</span> Key <span class="token operator">|</span> Default <span class="token operator">|</span> Extra          <span class="token operator">|</span>
+-------+---------------------+------+-----+---------+----------------+
<span class="token operator">|</span> name  <span class="token operator">|</span> text                <span class="token operator">|</span> YES  <span class="token operator">|</span>     <span class="token operator">|</span> NULL    <span class="token operator">|</span>                <span class="token operator">|</span>
<span class="token operator">|</span> age   <span class="token operator">|</span> int<span class="token punctuation">(</span><span class="token number">11</span><span class="token punctuation">)</span>             <span class="token operator">|</span> YES  <span class="token operator">|</span>     <span class="token operator">|</span> NULL    <span class="token operator">|</span>                <span class="token operator">|</span>
<span class="token operator">|</span> <span class="token function">id</span>    <span class="token operator">|</span> bigint<span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span> unsigned <span class="token operator">|</span> NO   <span class="token operator">|</span> PRI <span class="token operator">|</span> NULL    <span class="token operator">|</span> auto_increment <span class="token operator">|</span>
+-------+---------------------+------+-----+---------+----------------+
<span class="token number">3</span> rows <span class="token keyword">in</span> <span class="token builtin class-name">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>

<span class="token comment"># 添加一条数据</span>
insert into user <span class="token punctuation">(</span>name, age<span class="token punctuation">)</span> values <span class="token punctuation">(</span><span class="token string">'uphg'</span>, <span class="token number">18</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment"># 查询该列表数据</span>
<span class="token keyword">select</span> * from user<span class="token punctuation">;</span>
+----------+------+----+
<span class="token operator">|</span> name     <span class="token operator">|</span> age  <span class="token operator">|</span> <span class="token function">id</span> <span class="token operator">|</span>
+----------+------+----+
<span class="token operator">|</span> xiaofang <span class="token operator">|</span>   <span class="token number">18</span> <span class="token operator">|</span>  <span class="token number">1</span> <span class="token operator">|</span>
<span class="token operator">|</span> xiaohong <span class="token operator">|</span>   <span class="token number">18</span> <span class="token operator">|</span>  <span class="token number">2</span> <span class="token operator">|</span>
<span class="token operator">|</span> uphg     <span class="token operator">|</span>   <span class="token number">18</span> <span class="token operator">|</span>  <span class="token number">3</span> <span class="token operator">|</span>
+----------+------+----+
<span class="token number">3</span> rows <span class="token keyword">in</span> <span class="token builtin class-name">set</span> <span class="token punctuation">(</span><span class="token number">0.00</span> sec<span class="token punctuation">)</span>
</code></pre></div><h3 id="关于问题搜索"><a href="#关于问题搜索" class="header-anchor">#</a> 关于问题搜索</h3> <p>搜索判断是否存在语句</p> <div class="language- extra-class"><pre class="language-text"><code>mysql 5.7 create database if not exists
</code></pre></div><p>搜索关于 utf8mb4 编码的问题</p> <div class="language-sh extra-class"><pre class="language-sh"><code>mysql <span class="token number">5.7</span> create database utf8mb4
</code></pre></div><h2 id="推荐文档"><a href="#推荐文档" class="header-anchor">#</a> 推荐文档</h2> <p>在 <a href="https://devdocs.io/" target="_blank" rel="noopener noreferrer">https://devdocs.io/<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> 中搜索 PostgreSQL 添加其中的 v11 版本到自己的常用文档（Enable）。</p></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新：</span><span class="time">2021-04-11 13:01</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><svg aria-hidden="true" class="icon"><use xlink:href="#icon-left"></use></svg> <a href="/docs/node/fanyi.html" class="prev">命令行翻译工具</a></span> <!----></p></div> </main></div><div class="global-ui"><!----><!----></div></div>
    <script src="/docs/assets/js/app.cdec4db9.js" defer></script><script src="/docs/assets/js/2.993bd611.js" defer></script><script src="/docs/assets/js/95.4f07ca32.js" defer></script><script src="/docs/assets/js/7.5a12abef.js" defer></script>
  </body>
</html>
